import axios from 'axios';

let base = 'http://localhost:8088/codeing';

// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
    config => {
        if (localStorage.getItem('Authorization')) {
            config.headers.Authorization = localStorage.getItem('Authorization');
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    response => {
        if (response) {
            if (response.data.code === 401) {
                window.vm.$router.push({path: '/login'});
            }
        }
        return response;
    },
    error => {
        return Promise.reject(error.response.data)   // 返回接口返回的错误信息
    });

export const requestLogin = params => {
    return axios.post(`/api/codeing/sysUser/login`, params).then(res => res.data);
};

// 用户管理
export const getUserList = params => {
    return axios.get(`${base}/user/list`, {params: params});
};

export const getUserListPage = params => {
    return axios.post(`/api/codeing/sysUser/pageList`, params);
};

export const removeUser = params => {
    return axios.get(`/api/codeing/sysUser/delete/` + params);
};

export const batchRemoveUser = params => {
    return axios.get(`/api/codeing/sysUser/batchDelete`, {params: params});
};

export const editUser = params => {
    return axios.post(`/api/codeing/sysUser/update`, params);
};

export const addUser = params => {
    return axios.post(`/api/codeing/sysUser/insert`, params);
};

export const getRolesByUserId = params => {
    return axios.get(`/api/codeing/sysRole/getByUserId/` + params);
};

export const getAllRoles = () => {
    return axios.get(`/api/codeing/sysRole/findAll/`);
};

// 角色管理
export const getRoleListPage = params => {
    return axios.post(`/api/codeing/sysRole/pageList`, params);
};

export const removeRole = params => {
    return axios.get(`/api/codeing/sysRole/delete/` + params);
};

export const batchRemoveRole = params => {
    return axios.get(`/api/codeing/sysRole/batchDelete`, {params: params});
};

export const editRole = params => {
    return axios.post(`/api/codeing/sysRole/update`, params);
};

export const addRole = params => {
    return axios.post(`/api/codeing/sysRole/insert`, params);
};

export const getPermission = params => {
    return axios.get(`/api/codeing/sysRole/getAllPrivilege/` + params);
};

export const updatePermission = params => {
    return axios.post(`/api/codeing/sysRolePrivilege/updatePermission`, params);
};

export const getUserInfo = () => {
    return axios.get(`/api/codeing/sysUser/info`);
};

export const queryHitByPage = params => {
    return axios.post(`/api/codeing/es/queryHitByPage`, params);
};

// 文章管理
export const getArticleListPage = params => {
    return axios.post(`/api/codeing/article/pageList`, params);
};

export const removeArticle = params => {
    return axios.get(`/api/codeing/article/delete/` + params);
};

export const batchRemoveArticle = params => {
    return axios.get(`/api/codeing/article/batchDelete`, {params: params});
};

export const editArticle = params => {
    return axios.post(`/api/codeing/article/update`, params);
};

export const addArticle = params => {
    return axios.post(`/api/codeing/article/insert`, params);
};
